// Fill out your copyright notice in the Description page of Project Settings.

#include "Isalong2.h"
#include "HoldHand.h"
#include "DustWall.h"
#include "HoldSwordBase.h"
#include "TookUpItemBase.h"
#include "ElecUnitBase.h"
#include "WeaponGunBase.h"
#include "InteractItem.h"

UDataTable* AInteractItem::_gunData = nullptr;
UDataTable* AInteractItem::_holdData = nullptr;
UDataTable* AInteractItem::_elecData = nullptr;
UDataTable* AInteractItem::_buildingData = nullptr;
// Sets default values
AInteractItem::AInteractItem()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	_gunData = LoadObject<UDataTable>(NULL, TEXT("DataTable'/Game/BrainInVst/DataTables/GunData.GunData'"));
	_holdData = LoadObject<UDataTable>(NULL, TEXT("DataTable'/Game/BrainInVst/DataTables/HoldData.HoldData'"));
	_elecData = LoadObject<UDataTable>(NULL, TEXT("DataTable'/Game/BrainInVst/DataTables/ElecDT.ElecDT'"));
	_buildingData = LoadObject<UDataTable>(NULL, TEXT("DataTable'/Game/BrainInVst/DataTables/BuildingDT.BuildingDT'"));

	_itemGUID= FGuid::NewGuid();
}

// Called when the game starts or when spawned
void AInteractItem::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AInteractItem::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}

FHitResult AInteractItem::ToolRayTrace(FVector from, FVector to, bool isDebug, bool complex,bool returnPhyMat)
{
	FCollisionQueryParams traceParams(FName(TEXT("ITEMLINETRACE")), complex, this);
	traceParams.bTraceComplex = complex;
	traceParams.AddIgnoredActor(this);
	if (returnPhyMat)
	{
		traceParams.bReturnPhysicalMaterial = true;
	}

	FHitResult result;
	GetWorld()->LineTraceSingleByChannel(result, from, to, ECollisionChannel::ECC_Visibility, traceParams);
	//GetWorld()->LineTraceSingle(result, from, to,ECC_Visibility, traceParams);
	if (isDebug)
	{
		DrawDebugLine(GetWorld(), from, to, FColor::Red, false, 1.0f);
	}
	return result;
}

TArray<FHitResult> AInteractItem::ToolRayTraceMany(FVector from, FVector to, bool isDebug, bool complex)
{
	FCollisionQueryParams traceParams(FName(TEXT("ITEMLINETRACE")), complex, this);
	traceParams.bTraceComplex = complex;
	traceParams.AddIgnoredActor(this);

	TArray<FHitResult> result;
	GetWorld()->LineTraceMultiByChannel(result, from, to, ECollisionChannel::ECC_Visibility, traceParams);
	//GetWorld()->LineTraceSingle(result, from, to,ECC_Visibility, traceParams);
	if (isDebug)
	{
		DrawDebugLine(GetWorld(), from, to, FColor::Red, false, 1.0f);
	}
	return result;
}

AInteractItem* AInteractItem::ConstructInteractItemByID(AActor* createrContext, int id)
{
	auto world = createrContext->GetWorld();
	switch (id)
	{
	case ITEMTYPE_hand:
		return world->SpawnActor<AHoldHand>(AHoldHand::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
	case ITEMTYPE_m9:
	{
		AWeaponGunBase* temp = world->SpawnActor<AWeaponGunBase>(AWeaponGunBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_gunData->FindRow<FInteractData>(FName("Gun_M9"), FString("")), ITEMTYPE_m9);
		return temp;
	}
	case ITEMTYPE_magrum:
	{
		AWeaponGunBase* temp = world->SpawnActor<AWeaponGunBase>(AWeaponGunBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_gunData->FindRow<FInteractData>(FName("Gun_Magrum"), FString("")), ITEMTYPE_magrum);
		return temp;
	}
	case ITEMTYPE_bow :
	{
		AWeaponGunBase* temp = world->SpawnActor<AWeaponGunBase>(AWeaponGunBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_gunData->FindRow<FInteractData>(FName("Gun_Bow"), FString("")), ITEMTYPE_bow);
		return temp;
	}
	case ITEMTYPE_dustWall:
	{
		AWeaponBuildingBase* temp = world->SpawnActor<AWeaponBuildingBase>(AWeaponBuildingBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_buildingData->FindRow<FInteractData>(FName("Building_DustWall"), FString("")), ITEMTYPE_dustWall);
		return temp;
	}
	case ITEMTYPE_campFire:
	{
		AWeaponBuildingBase* temp = world->SpawnActor<AWeaponBuildingBase>(AWeaponBuildingBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_buildingData->FindRow<FInteractData>(FName("Building_CampFire"), FString("")), ITEMTYPE_campFire);
		return temp;
	}
	case ITEMTYPE_wireBox:
	{
		AElecUnitBase* temp= world->SpawnActor<AElecUnitBase>(AElecUnitBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_elecData->FindRow<FInteractData>(FName("Elec_WireBox"), FString("")), id);
		return temp;
	}
	case ITEMTYPE_baseballBat:
	{
		AHoldSwordBase* temp = world->SpawnActor<AHoldSwordBase>(AHoldSwordBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_holdData->FindRow<FInteractData>(FName("Hold_BaseballBat"), FString("")), id);
		return temp;
	}
	case ITEMTYPE_crowbar:
	{
		AHoldSwordBase* temp = world->SpawnActor<AHoldSwordBase>(AHoldSwordBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_holdData->FindRow<FInteractData>(FName("Hold_Crowbar"), FString("")), id);
		return temp;
	}
	case ITEMTYPE_fireAxe:
	{
		AHoldSwordBase* temp = world->SpawnActor<AHoldSwordBase>(AHoldSwordBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_holdData->FindRow<FInteractData>(FName("Hold_FireAxe"), FString("")), id);
		return temp;
	}
	case ITEMTYPE_golfClub:
	{
		AHoldSwordBase* temp = world->SpawnActor<AHoldSwordBase>(AHoldSwordBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_holdData->FindRow<FInteractData>(FName("Hold_GolfClub"), FString("")), id);
		return temp;
	}
	case ITEMTYPE_hatchet:
	{
		AHoldSwordBase* temp = world->SpawnActor<AHoldSwordBase>(AHoldSwordBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_holdData->FindRow<FInteractData>(FName("Hold_Hatchet"), FString("")), id);
		return temp;
	}
	case ITEMTYPE_kitchenKnife:
	{
		AHoldSwordBase* temp = world->SpawnActor<AHoldSwordBase>(AHoldSwordBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_holdData->FindRow<FInteractData>(FName("Hold_KitchenKnife"), FString("")), id);
		return temp;
	}
	case ITEMTYPE_pipeWrench:
	{
		AHoldSwordBase* temp = world->SpawnActor<AHoldSwordBase>(AHoldSwordBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_holdData->FindRow<FInteractData>(FName("Hold_PipeWrench"), FString("")), id);
		return temp;
	}
	case ITEMTYPE_shovel:
	{
		AHoldSwordBase* temp = world->SpawnActor<AHoldSwordBase>(AHoldSwordBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_holdData->FindRow<FInteractData>(FName("Hold_Shovel"), FString("")), id);
		return temp;
	}
	case ITEMTYPE_edgehammer:
	{
		AHoldSwordBase* temp = world->SpawnActor<AHoldSwordBase>(AHoldSwordBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_holdData->FindRow<FInteractData>(FName("Hold_Edgehammer"), FString("")), id);
		return temp;
	}
	case ITEMTYPE_canTeen:
	{
		AHoldSwordBase* temp = world->SpawnActor<AHoldSwordBase>(AHoldSwordBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_holdData->FindRow<FInteractData>(FName("Hold_CanTeen"), FString("")), id);
		return temp;
	}
	case ITEMTYPE_waterCan:
	{
		AHoldSwordBase* temp = world->SpawnActor<AHoldSwordBase>(AHoldSwordBase::StaticClass(), FVector(0, 0, 0), FRotator(0, 0, 0));
		temp->SetUpData(_holdData->FindRow<FInteractData>(FName("Hold_WaterCan"), FString("")), id);
		return temp;
	}
	}
	return nullptr;
}

ATookUpItemBase* AInteractItem::ConstructTookUpItemByID(AActor* createrContext, int id, int count, FVector location, int32 additionValue)
{
	auto world = createrContext->GetWorld();
	ATookUpItemBase* item= world->SpawnActor<ATookUpItemBase>(ATookUpItemBase::StaticClass(), location, FRotator(0, 0, 0));
	item->AdditionValue = additionValue;
	item->ConstructItem(id, count);
	return item;
}

